package com.heima.article.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ArticleMapper;
import com.heima.article.service.ArticleService;
import com.heima.file.service.FileStorageService;
import com.heima.model.article.dto.ArticleHomeDto;
import com.heima.model.article.pojo.ApArticle;
import com.heima.model.article.pojo.ApArticleConfig;
import com.heima.model.article.pojo.ApArticleContent;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.utils.common.ContentDto;
import com.heima.model.wemedia.dto.WmNewsTransferDto;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper,ApArticle> implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;
    @Autowired
    private ApArticleContentMapper apArticleContentMapper;
    @Autowired
    private Configuration configuration;
    @Autowired
    private FileStorageService fileStorageService;

    @Override
    public List<ApArticle> load(ArticleHomeDto dto) {
        return articleMapper.load(dto);
    }

    @Override
    public List<ApArticle> loadmore(ArticleHomeDto dto) {
        return articleMapper.loadmore(dto);
    }

    @Override
    public List<ApArticle> loadnew(ArticleHomeDto dto) {
        return articleMapper.loadnew(dto);
    }

    /**
     * app端文章保存
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult<Long> saveArticle(WmNewsTransferDto dto) throws IOException, TemplateException {
        //保存文章基本信息
        ApArticle article = new ApArticle();
        article.setAuthorId(Long.valueOf(dto.getUserId()));
        article.setAuthorName(dto.getAuthorName());
        article.setTitle(dto.getTitle());
        article.setChannelName(dto.getChannelName());
        article.setChannelId(dto.getChannelId());
        article.setLayout(dto.getType());
        article.setFlag((byte) 0);
        article.setImages(dto.getImages());
        article.setLabels(dto.getLabels());
        article.setLikes(0);
        article.setViews(0);
        article.setComment(0);
        article.setCollection(0);
        //页面静态化
        //获取文章内容
        String content = dto.getContent();
        //解析文章内容
        List<ContentDto> contentDtos = JSON.parseArray(content, ContentDto.class);
        Template template = configuration.getTemplate("news.ftl");
        //准备数据模型
        Map<String, Object> contents = new HashMap<>();
        contents.put("contents", contentDtos);
        StringWriter stringWriter = new StringWriter();
        template.process(contents, stringWriter);
        //将静态页面内容保存至minio中
        String result = stringWriter.toString();
        String fileName = UUID.randomUUID() + ".html";
        String staticUrl = fileStorageService.uploadHtmlFile("", fileName, new ByteArrayInputStream(result.getBytes()));
        //获取静态页面url存到数据库中
        article.setStaticUrl(staticUrl);
        articleMapper.insert(article);
        //保存文章内容
        Long id = article.getId();
        ApArticleContent articleContent = new ApArticleContent();
        articleContent.setContent(content);
        articleContent.setArticleId(id);
        apArticleContentMapper.insert(articleContent);
        //保存文章配置
        ApArticleConfig articleConfig = new ApArticleConfig();
        articleConfig.setIsComment(true);
        articleConfig.setIsForward(true);
        articleConfig.setIsDown(false);
        articleConfig.setIsDelete(false);
        articleConfig.setArticleId(id);
        apArticleConfigMapper.insert(articleConfig);
        //返回文章id
        return ResponseResult.okResult(id);
    }

    /**
     * 根据id查询文章对象
     * @param id
     * @return
     */
    @Override
    public ApArticle findById(Long id) {
        return getById(id);
    }
}
